/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package smartclient.client;

import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.*;
import org.jdom.output.*;
import java.io.*;
import java.util.Map;
import java.util.TreeMap;
import org.jdom.input.SAXBuilder;

/**
 *
 * @author Paolo
 */
public class FileQuotedData {
    private LinkedList<FileQuoted> listFileQ;

    public int add(FileQuoted fileQ){
        int i;
        if(listFileQ.add(fileQ)){
            i = listFileQ.size() - 1;
            return i;
        }
        else
            return -1;
    }

    public int remove(int index){
        if (index < listFileQ.size()){
            listFileQ.remove(index);
            return 1;
        }
        else
            return -1;
    }

    public FileQuoted get (int index){
        FileQuoted fileQ = new FileQuoted();
        if (index < listFileQ.size()){
            fileQ = listFileQ.get(index);
            return fileQ;
        }
        else
            return null;
    }

    public int saveFileQuoted (FileQuoted fileQ) throws IOException{
        File fileXml = new File ("C:/file.xml");
        Map<QuoteSealed,Object> map = new TreeMap<QuoteSealed, Object>();
        map = fileQ.getQuote();

        if (!fileXml.exists()){
            fileXml.createNewFile();
            //Elemento radice
            Element root = new Element("file_list");
            //Documento
            Document document = new Document(root);
            Element item = new Element("file");

            Element name = new Element("name");
            name.setText(fileQ.getName());
            item.addContent(name);

            Element chunks = new Element("chunks");
            for (QuoteSealed obj : map.keySet()){
                Element chunk = new Element("chunk");
                chunk.setAttribute("index", Integer.toString(obj.getIndex()));
                chunk.setAttribute("md5", obj.getDigest());
                chunks.addContent(chunk);
            }
            item.addContent(chunks);

            root.addContent(item);
            //Creazione dell'oggetto XMLOutputter
            XMLOutputter outputter = new XMLOutputter();
            //Imposto il formato dell'outputter come "bel formato"
            outputter.setFormat(Format.getPrettyFormat());
            //Produco l'output sul file
            outputter.output(document, new FileOutputStream(fileXml));
            System.out.println("File creato:");
            //Stampo l'output anche sullo standard output
            outputter.output(document, System.out);
            return 1;
        }
        else{
            try {
                //Creo un SAXBuilder e con esco costruisco un document
                SAXBuilder builder = new SAXBuilder();
                Document document = builder.build(fileXml);
                //Prendo la radice
                Element root = document.getRootElement();

                Element itemNew = new Element("file");

                Element name = new Element("name");
                name.setText(fileQ.getName());
                itemNew.addContent(name);

                Element chunks = new Element("chunks");
                for (QuoteSealed obj : map.keySet()){
                    Element chunk = new Element("chunk");
                    chunk.setAttribute("index", Integer.toString(obj.getIndex()));
                    //per recuperare gli attributi dei chunk
                    //System.out.println(chunk.getAttributeValue("index"));
                    chunk.setAttribute("md5", obj.getDigest());
                    //System.out.println(chunk.getAttributeValue("md5"));
                    chunks.addContent(chunk);
                }
                itemNew.addContent(chunks);

                root.addContent(itemNew);

                //Creazione dell'oggetto XMLOutputter
                XMLOutputter outputter = new XMLOutputter();
                //Imposto il formato dell'outputter come "bel formato"
                outputter.setFormat(Format.getPrettyFormat());
                //Produco l'output sul file
                outputter.output(document, new FileOutputStream(fileXml));
                System.out.println("File aggiornato:");
                //Stampo l'output anche sullo standard output
                outputter.output(document, System.out);
                return 1;

            } catch (JDOMException ex) {
                Logger.getLogger(FileQuotedData.class.getName()).log(Level.SEVERE, null, ex);
            }
            return -1;
        }
    }



}
